#include"Queue.h"

void Queueinit(qp* s)
{
	assert(s);
	s->phead = NULL;
	s->ptail = NULL;
}

void QueueDestroy(qp* s)
{
	assert(s);
	qn* cur = s->phead;
	while (cur)
	{
		qn* next = cur->next;
		free(cur);
		cur = next;
	}
	s->phead = NULL;
	s->ptail = NULL;
}

void Queuepush(qp* s, Qdatatype x)
{
	assert(s);
	qn* Newnode = (qn*)malloc(sizeof(qn));
	if (Newnode == NULL)
	{
		perror("malloc");
		exit(1);
	}
	Newnode->data = x;
	Newnode->next = NULL;
	if (s->phead == NULL)
	{
		s->ptail = s->phead = Newnode;
	}
	else
	{
		s->ptail->next = Newnode;
		s->ptail = s->ptail->next;
	}
	Newnode = NULL;
}

void QueuePop(qp* s)
{
	assert(s);
	assert(s->phead);
	qn* tem = s->phead;
	s->phead = s->phead->next;
	if (s->phead == NULL)
	{
		s->ptail = NULL;
	}
	free(tem);
}

bool QueueEmpty(qp* s)
{
	assert(s);
	return !(s->phead);
}

int QueueSize(qp* s)
{
	qn* tem = s->phead;
	int n = 0;
	while (tem)
	{
		n++;
		tem = tem->next;
	}
	return n;
}

Qdatatype QueueFront(qp* s)
{
	assert(s);
	assert(s->phead);
	return s->phead->data;
}

Qdatatype QueueBack(qp* s)
{
	assert(s);
	assert(s->phead);
    return s->ptail->data;
}